//
//  CollectViewController.m
//  Cartoon
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 YSZ. All rights reserved.
//

#import "CollectViewController.h"
#import "MoreTableViewCell.h"
#import "CartoonDetailsModel.h"
#import "CartoonDetailsViewController.h"
#import "Recommend.h"
@interface CollectViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *array;
@property (nonatomic,strong)FMDatabase *db;
@end

@implementation CollectViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SelfViewWidth, SelfViewHeight - 64 -49) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.db open];
   
    [self.tableView registerClass:[MoreTableViewCell class] forCellReuseIdentifier:@"reuse"];
    [self.tableView reloadData];
      self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    NSString *dbPath = [NSString stringWithFormat:@"%@/Documents/1.rdb",NSHomeDirectory()];
    
    self.db = [FMDatabase databaseWithPath:dbPath];
    
    BOOL isOpen = [self.db open];
    
    if (!isOpen) {
        NSLog(@"打开失败");
    }else{
        
        [self handleArrayData];
     
    }
    
    
    
    // Do any additional setup after loading the view.
}

- (void) handleArrayData
{
    FMResultSet *set = [self.db executeQuery:@"SELECT * FROM Cartoon"];
    self.array = [NSMutableArray array];
    
    while ([set next]) {
        Recommend *stu = [[Recommend alloc]init];
        stu.title = [set stringForColumn:@"title"];
        stu.artist_name = [set stringForColumn:@"name"];
        stu.cover_url = [set stringForColumn:@"pic"];
        stu.lastup = [set stringForColumn:@"gx"];
        stu.grade = [[set stringForColumn:@"star"]integerValue];
        stu.comic_id = [set stringForColumn:@"idd"];
        [self.array addObject:stu];
        NSLog(@"%@2222",self.array);
    }
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
    

}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{    return self.array.count;
    
}
//重用池
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MoreTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];

    
    Recommend *car = [self.array objectAtIndex:indexPath.row];
    [cell.bigImageView sd_setImageWithURL:[NSURL URLWithString:car.cover_url] placeholderImage:[UIImage imageNamed:@"5"]];
    cell.titleLabel.text = car.title;
    cell.namelabel.text = [NSString stringWithFormat:@"作者: %@",car.artist_name];
    cell.starView.starNum = car.grade;
    
    
    cell.lastupLabel.text = [NSString stringWithFormat:@"更新到%@",car.lastup];
    
    return cell;
    
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    
    [super setEditing:editing animated:animated];
    
    [_tableView setEditing:editing animated:YES];
    
    
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        CartoonDetailsModel *deta = _array[indexPath.row];
        
        [self.db open];
        [self.db executeUpdate:[NSString stringWithFormat:@"delete from Cartoon where idd = '%@'",deta.comic_id]];
        [_array removeObjectAtIndex:indexPath.row];
        [self.tableView reloadData];
        
        NSLog(@"%@",deta.comic_id);
        
    }
    

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CartoonDetailsViewController  *carVC = [[CartoonDetailsViewController alloc]init];
    
    Recommend *DBM = [self.array objectAtIndex:indexPath.row];

    carVC.idtext = DBM.comic_id;
  [self.navigationController pushViewController:carVC animated:YES];}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
